package com.slz3.business.module.drawManagement.draIntegralLottery.domain;

import com.slz3.business.frame.enums.AuditFlag;
import com.slz3.business.frame.enums.UsingFlag;
import com.slz3.business.frame.redis.Jedis;
import com.slz3.business.frame.redis.RK;
import com.slz3.business.frame.steward.Steward;
import com.slz3.business.frame.util.LocalDT;
import com.slz3.business.frame.util.UUIDUtils;
import com.slz3.business.frame.util.VE;
import com.slz3.business.module.drawManagement.draIntegralLottery.enums.DraIntegralLotteryGoodsDrawPeopleFlag;
import org.springframework.util.MultiValueMap;

import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

public class DraIntegralLotteryFactory {

    /**
     * 初始化基础属性值
     */
    public static DraIntegralLottery initBaseFields(DraIntegralLottery draIntegralLottery) {
        if (draIntegralLottery.getUuid() == null) {
            draIntegralLottery.setUuid(UUIDUtils.generateUUID());
            draIntegralLottery.setCreateTime(LocalDateTime.now());
            if (VE.isEmpty(draIntegralLottery.getSerAudit())) {
                draIntegralLottery.setSerAudit(AuditFlag.WAIT_SUBMIT);
            }
            if (VE.isEmpty(draIntegralLottery.getSerUsing())) {
                draIntegralLottery.setSerUsing(UsingFlag.ENABLE);
            }
        } else {
            draIntegralLottery.setLastModifyTime(LocalDateTime.now());
        }
        return draIntegralLottery;
    }

    /**
     * 初始化基础属性值
     */
    public static DraIntegralLottery initBaseFields() {
        return initBaseFields(newTemStyle());
    }

    public static DraIntegralLottery newTemStyle() {
        return new DraIntegralLottery();
    }

    public static DraIntegralLotteryCondition newDraIntegralLotteryCondition(MultiValueMap<String, String> params) {

        DraIntegralLotteryCondition condition = new DraIntegralLotteryCondition();

        String createTimeOUTSET = params.getFirst("createTimeOUTSET");
        String createTimeFINISH = params.getFirst("createTimeFINISH");
        String lastModifyTimeOUTSET = params.getFirst("lastModifyTimeOUTSET");
        String lastModifyTimeFINISH = params.getFirst("lastModifyTimeFINISH");
        String serAuditList = params.getFirst("serAuditList");

        condition.setCreateTimeOUTSET(VE.isEmpty(createTimeOUTSET) ? null : LocalDT.parseStringToDateTime(createTimeOUTSET, DraIntegralLotteryCondition.CREATE_TIME_OF_PATTERN));
        condition.setCreateTimeFINISH(VE.isEmpty(createTimeFINISH) ? null : LocalDT.parseStringToDateTime(createTimeFINISH, DraIntegralLotteryCondition.CREATE_TIME_OF_PATTERN));
        condition.setLastModifyTimeOUTSET(VE.isEmpty(lastModifyTimeOUTSET) ? null : LocalDT.parseStringToDateTime(lastModifyTimeOUTSET, DraIntegralLotteryCondition.CREATE_TIME_OF_PATTERN));
        condition.setLastModifyTimeFINISH(VE.isEmpty(lastModifyTimeFINISH) ? null : LocalDT.parseStringToDateTime(lastModifyTimeFINISH, DraIntegralLotteryCondition.CREATE_TIME_OF_PATTERN));
        condition.setSerAuditList(VE.isEmpty(serAuditList) ? null : Arrays.stream(serAuditList.split(",")).map(item -> AuditFlag.valueOf(item)).collect(Collectors.toList()));

        return condition;

    }


    public static void saveDraIntegralLotteryNumber(DraIntegralLottery draIntegralLotteryDBLatest) {

        DraIntegralLotteryGoodsDrawPeopleFlag serGoodsDrawPeople = draIntegralLotteryDBLatest.getSerGoodsDrawPeople();

        Map dataMap = new HashMap();
        UUID uuid = draIntegralLotteryDBLatest.getUuid();
        if (DraIntegralLotteryGoodsDrawPeopleFlag.SHIRENTUAN.equals(serGoodsDrawPeople)) {
            for (int i = 0; i < 10; i++) {
                dataMap.put("\"1000" + i+"\"","1");
                String num = "1000" + i;
                Jedis.db0.lpush(String.format("%s:%s:%s:%s:%s", RK.CONFIG_DOMIN, RK.DRA_INTEGRAL_LOTTERY, RK.NUMBER, uuid, RK.LIST), num);
            }
            Jedis.db0.hmSetMap(String.format("%s:%s:%s:%s", RK.CONFIG_DOMIN, RK.DRA_INTEGRAL_LOTTERY, RK.NUMBER, uuid), dataMap);
        }
        if (DraIntegralLotteryGoodsDrawPeopleFlag.BAIRENTUAN.equals(serGoodsDrawPeople)) {
            for (int i = 0; i < 100; i++) {
                if (i < 10) {
                    dataMap.put("\"1000" + i+"\"","1");
                    String num = "1000" + i;
                    Jedis.db0.lpush(String.format("%s:%s:%s:%s:%s", RK.CONFIG_DOMIN, RK.DRA_INTEGRAL_LOTTERY, RK.NUMBER, uuid, RK.LIST), num);
                } else {
                    dataMap.put("\"100" + i+"\"","1");
                    String num = "100" + i;
                    Jedis.db0.lpush(String.format("%s:%s:%s:%s:%s", RK.CONFIG_DOMIN, RK.DRA_INTEGRAL_LOTTERY, RK.NUMBER, uuid, RK.LIST), num);
                }
            }
            Jedis.db0.hmSetMap(String.format("%s:%s:%s:%s", RK.CONFIG_DOMIN, RK.DRA_INTEGRAL_LOTTERY, RK.NUMBER, uuid), dataMap);
        }
        if (DraIntegralLotteryGoodsDrawPeopleFlag.QIANRENTUAN.equals(serGoodsDrawPeople)) {
            for (int i = 0; i < 1000; i++) {
                if (i < 10) {
                    dataMap.put("\"1000" + i+"\"","1");
                    String num = "1000" + i;
                    Jedis.db0.lpush(String.format("%s:%s:%s:%s:%s", RK.CONFIG_DOMIN, RK.DRA_INTEGRAL_LOTTERY, RK.NUMBER, uuid, RK.LIST), num);
                } else if (i < 100 && i > 9) {
                    dataMap.put("\"100" + i+"\"","1");
                    String num = "100" + i;
                    Jedis.db0.lpush(String.format("%s:%s:%s:%s:%s", RK.CONFIG_DOMIN, RK.DRA_INTEGRAL_LOTTERY, RK.NUMBER, uuid, RK.LIST), num);
                } else {
                    dataMap.put("\"10" + i+"\"","1");
                    String num = "10" + i;
                    Jedis.db0.lpush(String.format("%s:%s:%s:%s:%s", RK.CONFIG_DOMIN, RK.DRA_INTEGRAL_LOTTERY, RK.NUMBER, uuid, RK.LIST), num);
                }
            }
            Jedis.db0.set(String.format("%s:%s:%s:%s", RK.CONFIG_DOMIN, RK.DRA_INTEGRAL_LOTTERY, RK.NUMBER, uuid), dataMap);
        }
    }
}
